#include "config_handler.h"

FILE * config_file;

char * configLabels[] = {"addsub_delay",
						"muldiv_delay",
						"instruction_q_depth",
						"addsub_rs",
						"muldiv_rs",
						"load_q_depth",
						"store_q_depth",
						"reorder_buffer",
						"ghr_width",
						"two_threads_enabled",
						"l1_block_size",
						"l1_access_delay",
						"l1_cache_size",
						"l2_block_size",
						"l2_access_delay",
						"l2_cache_size",
						"mem_access_delay"};

int parseConfigLine(char * line);

int parseConfigFile(char * filepath) {
	int i;
	char config_line[MAX_CONFIG_LINE_LENGTH];
	
	config_file= fopen(filepath, "r");
	if (config_file == NULL)
		return -1;

	printf("Loading configurations file...\n");

	while(fgets(config_line, MAX_CONFIG_LINE_LENGTH, config_file) != NULL) {

		if ((config_line[0] == '/') || (config_line[0] == '\n') || (isspace(config_line[0])))
			continue;
			
		parseConfigLine(config_line);
		
	}
	
	fclose(config_file);

	//for (i = 0 ; i < CONFIG_NUM ; i++) {
	//	printf("%s : %d\n",configLabels[i],config_values[i]);
	//}

	return 0;
}

int parseConfigLine(char * line) {
	
	char * label;
	char * value_str;
	int value;
	int label_index;

	label = strtok(line, "=");
	value_str = strtok(NULL, "//"); 
	value = -1;
	isNumeric(trimwhitespace(value_str),&value,0);

	if ((label_index = findMemberIndex(trimwhitespace(label), configLabels, CONFIG_NUM)) != -1)
		config_values[label_index] = value;
	
	return 0;

}

